/*
 *Copyright © 2015 zhaorongbao
 *招融宝
 *http://zhaorongbao.com
 *All rights reserved.
 */
package com.hanhai.common.web.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageInfo;
import com.hanhai.common.enums.ErrorCode;
import com.hanhai.common.util.CommonUtil;
import com.hanhai.common.util.CustomTimestampEditor;

public class BaseController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	
	public String getRemoteIp(HttpServletRequest request) {
		return CommonUtil.getRemoteIp(request);
	}

	protected void returnJsonObject(HttpServletResponse response,
			JSONObject jsonObject) {
		String jsonString = JSON.toJSONString(jsonObject,
				SerializerFeature.DisableCircularReferenceDetect);
		returnJsonString(response, jsonString);
	}
	protected void returnObjectJsonString(HttpServletResponse response,
			Object object) {
		String jsonStr = JSONObject.toJSONString(object);
	
		returnJsonString(response, jsonStr);
	}

	protected void returnJsonString(HttpServletResponse response,
			String content) {
		CommonUtil.returnMessage(response, content);
	}

	protected void jsonPage(HttpServletResponse response, PageInfo pageInfo) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("rows", pageInfo.getList());
		jsonObject.put("total", pageInfo.getTotal());
		returnJsonObject(response, jsonObject);
		
	}
	protected void jsonExist(HttpServletResponse response, Integer exist) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("exist", exist);
		jsonObject.put("code", ErrorCode.OK.getCode());
		returnJsonObject(response, jsonObject);
	}

	protected void jsonSucceed(HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", ErrorCode.OK.getCode());
		returnJsonObject(response, jsonObject);
	}
	
	protected void success(HttpServletResponse response,String msg){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		if(msg != null){
		   map.put("msg", msg);
		}
		String str = JSONObject.toJSONString(map);
		try {
			response.getWriter().write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void success(HttpServletResponse response){
		success(response,null);
	}
	protected void fail(HttpServletResponse response){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		String str = JSONObject.toJSONString(map);
		try {
			response.getWriter().write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void jsonSucceedCross(HttpServletResponse response,
			String callback) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", ErrorCode.OK.getCode());
		jsonObject.put("msg", "ok");
		String str = buildCrossStr(callback, jsonObject);
		returnJsonString(response, str);
	}

	protected void jsonFail(HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", ErrorCode.BIZ_ERROR.getCode());
		returnJsonObject(response, jsonObject);
	}

	protected void jsonFail(HttpServletResponse response, String msg) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", ErrorCode.BIZ_ERROR.getCode());
		jsonObject.put("msg", msg);
		returnJsonObject(response, jsonObject);
	}

	protected void jsonFailCross(HttpServletResponse response, String msg,
			String callback) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", ErrorCode.BIZ_ERROR.getCode());
		jsonObject.put("msg", msg);
		String str = buildCrossStr(callback, jsonObject);
		returnJsonString(response, str);
	}

	protected String buildCrossStr(String callback, JSONObject jsonObject) {
		String str = JSONObject.toJSONString(jsonObject);
		str = callback + "(" + str + ")";
		return str;
	}

	// 如果是按标准格式，是自动可以转，如果是其他格式才String接收
	@InitBinder
	protected void initBinder(WebDataBinder binder) throws ServletException {

		SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		datetimeFormat.setLenient(false);
		
		binder.registerCustomEditor(java.sql.Timestamp.class, new CustomTimestampEditor(datetimeFormat, true));
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);

		binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(dateFormat, true));

		CustomCollectionEditor collectionEditor = new CustomCollectionEditor(List.class, true);

		binder.registerCustomEditor(List.class, collectionEditor);
	}
}
